package com.music.common.core.utils.token;



import com.music.common.core.constant.Constants;
import com.music.common.core.utils.StringUtils;
import com.music.common.core.enums.ExceptionCode;
import com.music.common.core.enums.SysType;
import com.music.common.core.exception.CustomException;

import javax.servlet.http.HttpServletRequest;

public class TokenUtils {
    public static AccToken getToken(HttpServletRequest request, SysType systemType) {
        String header = request.getHeader(Constants.LOGIN_TOKEN_AUTH);
        if(StringUtils.isEmpty(header)||header.length()<7){
            throw new CustomException(ExceptionCode.UNAUTH);
        }

        String jwt = header.substring(Constants.JWT_PREFIX.length());
        AccToken token = null;
        switch (systemType) {
            case ADMIN:
                token = JwtTokenUtils.parseToken(jwt);
                break;
            case PORTAL:
                token = JwtTokenUtils.parseWebToken(jwt);
                break;
        }
        return token;
    }
}
